package inputstream01;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * FileInputStream成员方法
 *   int read(byte[] b)
 *           1.从文件中读取尽可能多的字节,装到字节数组中去,0索引存放文件中的第一个字节,1索引存放文件中的第二个字节,以此类推
 *           2.返回值代表每次从文件中读取的字节个数
 * String类的构造方法
 *   String(byte[] bytes)
 *           通过使用平台的默认字符集解码指定的 byte 数组，构造一个新的 String
 */
public class FileInputStream02 {
	public static void main(String[] args) throws IOException {
		//method01();1
		FileInputStream fis = new FileInputStream("day23/6.txt");

		byte[] bytes = new byte[2];

		/*int length = fis.read(bytes);
		System.out.println(length+" "+new String(bytes));//2 ab

		int length2 = fis.read(bytes);
		System.out.println(length2+" "+new String(bytes));//2 cd

		int length3 = fis.read(bytes);
		System.out.println(length3+" "+new String(bytes));//1 ed

		int length4 = fis.read(bytes);
		System.out.println(length4+" "+new String(bytes));//-1 ed

		int length5 = fis.read(bytes);
		System.out.println(length5+" "+new String(bytes));//-1 ed*/

		int length;
		while ((length=fis.read(bytes))!=-1) {
			System.out.println(new String(bytes,0,length));
		}


		fis.close();
	}

	private static void method01() throws IOException {
		FileInputStream fis = new FileInputStream("day23/6.txt");

		byte[] bytes = new byte[2];
		fis.read(bytes);
		/*for (byte b : bytes) {
			System.out.println((char) b);
		}*/
		System.out.println(new String(bytes));//"ab"
		fis.close();
	}
}
